import { useMemberStore } from '@/store/index.js'
const baseURL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'
const httpInterceptor = {
    invoke(options) {
        if (!options.url.startsWith('http')) {
          options.url = baseURL + options.url
        } 
        options.timeout = 10000
        options.header = { ...options.header, 'source-client': 'miniapp' }
        const memberStore = useMemberStore()
        const token = memberStore?.token
        if (token) {
          options.header.Authorization = token
        }
      }  
}
uni.addInterceptor('request', httpInterceptor)
//上传头像
uni.addInterceptor('uploadFile', httpInterceptor)
export const request = (options) => {
    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            success(res) {
                resolve(res.data)
            },
            fail(err) {
                reject(err)
            }
        })
    })
}